package com.hzsun.redis.config;

import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.*;

/**
 * @Author: DingJl
 * @Date: 2022/1/26 16:12
 * @Version 1.0
 */
public class ClusterRedisConfig {


    /**
     * jedis
     */
//    @Bean
    public RedisConnectionFactory jedisConnectionFactory() {
        int port=0;
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        RedisNode  node1=new RedisClusterNode("host",port);
        RedisNode  node2=new RedisClusterNode("host",port);
        Iterable<RedisNode> iterable = Arrays.asList(node1,node2);
        redisClusterConfiguration.setClusterNodes(iterable);
        return new JedisConnectionFactory(redisClusterConfiguration);
    }

    /**
     * Lettuce
     */
//    @Bean
    public RedisConnectionFactory lettuceConnectionFactory() {
        int port=0;
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        RedisNode  node1=new RedisClusterNode("host",port);
        RedisNode  node2=new RedisClusterNode("host",port);
        Iterable<RedisNode> iterable = Arrays.asList(node1,node2);
        redisClusterConfiguration.setClusterNodes(iterable);
        return new LettuceConnectionFactory(redisClusterConfiguration);
    }

    //    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return  redisTemplate;
    }
}
